package com.offcn.question.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.offcn.question.entity.TypeEntity;
import com.offcn.question.service.TypeService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.offcn.question.entity.QuestionEntity;
import com.offcn.question.service.QuestionService;
import com.offcn.common.utils.PageUtils;
import com.offcn.common.utils.R;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;


/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-07-21 23:00:49
 */
@RestController
@RequestMapping("question/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = questionService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		QuestionEntity question = questionService.getById(id);

        return R.ok().put("question", question);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody QuestionEntity question){
		questionService.save(question);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody QuestionEntity question){
		questionService.updateById(question);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		questionService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }






    @PostMapping("/upload")
    public R upload(MultipartFile file){
        Map map = questionService.upload(file);
        return R.ok().put("result",map.get("result"))
                .put("msg",map.get("msg")).put("num",map.get("num"));

    }

    @GetMapping("/download")
    public void download(String tableName, HttpServletResponse response){
        Workbook workbook = questionService.download();
        if (workbook!=null){
           String fileName = "uxue_"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+".xlsx";
            response.setHeader("Content-Disposition","attachment;filename="+fileName);
            response.setContentType("application/octet-stream;charset=GB2312");
            response.containsHeader("GB2312");
            OutputStream outputStream;
            try {
                outputStream = response.getOutputStream();
                workbook.write(outputStream);
                outputStream.flush();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            try {
                response.getWriter().print("error");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
    @Resource
    private TypeService typeService;

    @RequestMapping("/countQuestion")
    public R countQuestion(){
        List<Map<String, Object>> mapList = questionService.countQuestion();
        //遍历map集合
        for (Map<String, Object> map : mapList) {
           //获取map的值
           Long type =(Long) map.get("TYPE");
           //通过id去type表查询type对象
            TypeEntity typeEntity = typeService.getById(type);
            //在存入map集合
            map.put("name",typeEntity.getType());
        }
        return R.ok().put("mapList", mapList);
    }

}
